package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author chencfgs
 * @date 2019-12-02 15:37
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public void addSetmeal(Setmeal setmeal) {
        setmealDao.addSetmeal(setmeal);
    }

    @Override
    public void addIds(Integer id, Integer checkgroupId) {
        setmealDao.addIds(id, checkgroupId);
    }

    @Override
    public Page<Setmeal> findPage(QueryPageBean param) {
        String queryString = param.getQueryString();
        if (queryString != null) {
            queryString = "%" + queryString + "%";
        }
        Page<Setmeal> page = setmealDao.findPage(queryString);
        return page;
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        if (queryString != null) {
            queryString = "%" + queryString + "%";
        }
        Page<Setmeal> page = setmealDao.findPage(queryString);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.addSetmeal(setmeal);
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            //绑定套餐和检查组的多对多关系
            setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);


        }
        //将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        List<Setmeal> list = setmealDao.findAllSetmeal();
        return list;
    }

    /*@Override
    public Setmeal findById(String id) {
        Setmeal setmeal = setmealDao.findById(Integer.parseInt(id));
        return setmeal;
    }*/
    public String findById(String id) {
        Jedis jedis = jedisPool.getResource();

        //查询redis并判断是否有键名与id是相同的，如果没有，查询数据库；如果有，直接从redis取
        String s = jedis.get(id);

        //redis中没有
        if (s == null || s.length() <= 0) {
            //查询出的套餐信息转换为json后，存入redis并发送到controller
            Setmeal setmeal = setmealDao.findById(Integer.parseInt(id));
            String setmealToString = JSONObject.toJSON(setmeal).toString();
            jedis.set(id, setmealToString);
            //返回
            return setmealToString;
        }

        jedis.close();

        //redis中有，直接返回
        return s;

    }

    @Override
    public List<String> findAllSetmealName() {
        List<String> list = setmealDao.findAllSetmealName();
        return list;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        List<Map<String, Object>> mapList = setmealDao.findSetmealCount();
        return mapList;
    }


    //绑定套餐和检查组的多对多关系
    private void setSetmealAndCheckGroup(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmeal_id", id);
            map.put("checkgroup_id", checkgroupId);
            setmealDao.setSetmealAndCheckGroup(map);
        }
    }

    private void savePic2Redis(String pic) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, pic);
    }

}